package demo.lifecycle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.*;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author created by wangyang
 * email:techyang.wangy@ymm56.com
 * company: yunmanman
 * @date 2018/8/27 下午2:04
 */
@Service
public class HelloComponentLifeCycle implements BeanNameAware, BeanClassLoaderAware,
        BeanFactoryAware, ResourceLoaderAware, ApplicationEventPublisherAware, MessageSourceAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloComponentLifeCycle.class);
    private String hello = "hello";

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("invoke BeanClassLoaderAware");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("invoke beanFactoryAware");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("invoke BeanNameAware");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("invoke DisposableBean destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("invoke InitializingBean afterPropertiesSet");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("invoke ApplicationContextAware");
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println("invoke ApplicationEventPublisherAware");
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println("invoke MessageSourceAware");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("invoke ResourceLoaderAware");
    }

    @PostConstruct
    public void postConstruct() {
        LOGGER.info("invoke postConstruct");
    }

    public HelloComponentLifeCycle() {
        System.out.println("invoke HelloComponent");
        System.out.println(hello);
    }

    @PreDestroy
    public void preDestroy() {
        LOGGER.info("invoke preDestroy");
    }

    public String getHello(){
        return hello;
    }
}
